package org.gruposp2p.proyectokarma.gwt.client.ui.vote;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.AsyncDataProvider;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.Range;
import org.gruposp2p.proyectokarma.gwt.client.domain.Vote;
import org.gruposp2p.proyectokarma.gwt.client.domain.KarmaParameter;
import org.gruposp2p.proyectokarma.gwt.client.event.VoteEvent;
import org.gruposp2p.proyectokarma.gwt.client.event.EventBusManager;
import org.gruposp2p.proyectokarma.gwt.client.ui.ErrorDialog;
import org.gruposp2p.proyectokarma.gwt.client.util.DateUtils;
import org.gruposp2p.proyectokarma.gwt.client.util.RequestHelper;
import org.gruposp2p.proyectokarma.gwt.client.util.ServerPaths;
import com.allen_sauer.gwt.log.client.Log;
	
public class VotesTablePanel extends Composite 
	implements VoteEvent.Handler {

    @UiField CellTable<Vote> cellTable;
    @UiField SimplePager simplePager;
    VoteListAsyncAdapter adapter;
    private int offset = 0;
    private int cellTablePageSize = 20;
    
    ArrayList<Vote> values;

    private static VotesTablePanelUiBinder uiBinder = 
    	GWT.create(VotesTablePanelUiBinder.class);

    interface VotesTablePanelUiBinder extends UiBinder<Widget, VotesTablePanel> { }

    public VotesTablePanel() {
		initWidget(uiBinder.createAndBindUi(this));
        cellTable.setPageSize(cellTablePageSize);
        cellTable.addColumn(new Column<Vote, String>(new TextCell()) {
            @Override
            public String getValue(Vote vote) {
              return "";
            }
          }, "Fecha");
        cellTable.addColumn(new Column<Vote, String>(new TextCell()) {
            @Override
            public String getValue(Vote vote) {
              //return user.email;
            	return "";
            }
          }, "Documento"); 
        
        cellTable.addColumn(new Column<Vote, String>(new TextCell()) {
            @Override
            public String getValue(Vote vote) {
              //return DateUtils.getSimpleStringFromDate(user.dateOfBird);
            	return "";
            }
          }, "Usuario"); 
        cellTable.addColumn(new Column<Vote, String>(new TextCell()) {
            @Override
            public String getValue(Vote vote) {
            	return "";
            }
          }, "Valor");
        
        adapter = new VoteListAsyncAdapter();
        adapter.addDataDisplay(cellTable);
        //adapter.updateRowData(0, values);
        EventBusManager.getInstance().registerToVoteEvent(this);
    }
    
    protected class VoteListAsyncAdapter extends AsyncDataProvider<Vote>{
        @Override
        protected void onRangeChanged(HasData<Vote> view) {
			Range newRange = view.getVisibleRange();
			//loadData(newRange.getStart(), newRange.getLength());
        }
    } 
    


	@Override
	public void onVoteChanged(VoteEvent event) {
	}
	
    private class ServerRequestCallback implements RequestCallback {

        @Override
        public void onError(Request request, Throwable exception) {
        	new ErrorDialog().show("Exception", exception.getMessage());
        	Log.error(exception.getMessage());                
        }

        @Override
        public void onResponseReceived(Request request, Response response) {
        }

    }

}